﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Globalization;
namespace HF.Framework
{
   public class HFCommon
    {
       /// <summary>
       /// 给定2位数字加1后输出2位数字，不足两位前面加0
       /// </summary>
       /// <param name="strId">原数字</param>
       public static string GetNextId2(string strId)
       {
           if (string.IsNullOrEmpty(strId)) return "00";
           int num = Convert.ToInt16(strId);
           num++;
           if (num < 10)
           {
               return "0" + num.ToString();
           }
           else
           {
               return num.ToString();
           }
       }
       /// 获得当前日期,yyyymmdd
       /// </summary>
       /// <returns></returns>
       public static string GetNowDate1()
       {
           return DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
       }
       /// <summary>
       /// 获得当前日期后n天add日期,yyyy-mm-dd
       /// </summary>
       /// <returns></returns>
       public static string GetAddDaysDate(int day)
       {
           return DateTime.Now.AddDays(day).ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo);
       }
       /// <summary>
       /// 从IIS服务器下载文件
       /// </summary>
       /// <param name="remoteFileName">远程文件地址</param>
       /// <param name="localFileName">本地文件名</param>
       /// <returns></returns>
       public static bool DownloadFile(string remoteFileName, string localFileName)
       {
           WebClient myclient = new WebClient();
           try
           {
               myclient.DownloadFile(remoteFileName, localFileName);
               return true;
           }
           catch
           {
               return false;
           }
       }
       //// <summary>
       /// 上传文件至IIS服务器
       /// </summary>
       /// <param name="localFileName">文件名，全路径格式</param>
       /// <param name="remoteFileName">上传到文件服务器的文件名</param>
       public static bool UpLoadFile(string localFileName, string remoteFileName)
       {
           WebClient myWebClient = new WebClient();
           myWebClient.Credentials = CredentialCache.DefaultCredentials;
           // 将要上传的文件打开读进文件流
           FileStream myFileStream = new FileStream(localFileName, FileMode.Open, FileAccess.Read);
           BinaryReader myBinaryReader = new BinaryReader(myFileStream);
           byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
           Stream postStream = myWebClient.OpenWrite(remoteFileName, "PUT");
           try
           {
               if (postStream.CanWrite)
               {
                   postStream.Write(postArray, 0, postArray.Length);
               }
               else
               {
                   return false;
               }
               postStream.Close();//关闭流
               return true;
           }
           catch
           {
               postStream.Close();//关闭流
               return false;

           }
       }
    }
}
